//
//  TCUUserNotification.m
//  TreeMenuTest
//
//  Created by Etienne on 11/07/07.
//  Copyright (c) 2007 Etienne Samson. All rights reserved.
//

#import "TCUCFUserNotification.h"

#import "CFRuntime_internal.h"

#import <objc/objc-runtime.h>

#import "TCUUserNotification.h"

NSString * const NSUserNotificationIconURLKey;
NSString * const NSUserNotificationSoundURLKey;
NSString * const NSUserNotificationLocalizationURLKey;
NSString * const NSUserNotificationAlertHeaderKey;/* = (NSString *)kCFUserNotificationAlertHeaderKey;*/
NSString * const NSUserNotificationAlertMessageKey;/* = (NSString *)kCFUserNotificationAlertMessageKey;*/
NSString * const NSUserNotificationDefaultButtonTitleKey;
NSString * const NSUserNotificationAlternateButtonTitleKey;
NSString * const NSUserNotificationOtherButtonTitleKey;
NSString * const NSUserNotificationProgressIndicatorValueKey;
NSString * const NSUserNotificationPopUpTitlesKey;
NSString * const NSUserNotificationTextFieldTitlesKey;
NSString * const NSUserNotificationCheckBoxTitlesKey;
NSString * const NSUserNotificationTextFieldValuesKey;
NSString * const NSUserNotificationPopUpSelectionKey;

static id _delegate = nil;

void asynchronousCallBack (CFUserNotificationRef userNotification, CFOptionFlags responseFlags)
{
    objc_msgSend(_delegate, @selector(userNotification:withResponseFlags:), userNotification, responseFlags);
}

@implementation TCUUserNotification

#pragma mark -
#pragma mark Custom Toll-Free Bridging
- (CFTypeID) _cfTypeID {
    return CFUserNotificationGetTypeID();
}

+ (void) load
{
    _CFRuntimeInitializeClassForBridging(CFUserNotificationGetTypeID());
    if (_CFRuntimeSetupBridging(CFUserNotificationGetTypeID(), [TCUCFUserNotification class], [TCUCFUserNotification__ class]))
        NSLog(@"Successfully setup bridge for TCUCFUserNotification...");
    else
        NSLog(@"Failed _CFRuntimeSetupBridging()");
}

+ (int) displayAlert:(NSString*)title message:(NSString*)message timeout:(double)timeout
{
    CFOptionFlags responseFlags = 0;
    CFUserNotificationDisplayAlert(timeout, 0, NULL, NULL, NULL, (CFStringRef)title, (CFStringRef)message, NULL, NULL, NULL, &responseFlags);
    
    return responseFlags;
        
}

+ (void) displayNotice:(NSString*)title message:(NSString*)message timeout:(double)timeout
{
    CFUserNotificationDisplayNotice(timeout, 0, NULL, NULL, NULL, (CFStringRef)title, (CFStringRef)message, NULL);
}

+ (id) notificationWithDictionary:(NSDictionary*)dict
{
    return [[[TCUUserNotification alloc] initWithDictionary:dict] autorelease];
}

- (id) initWithDictionary:(NSDictionary*)dict
{
    SInt32 error = 0;
    
    self = (TCUUserNotification*)CFUserNotificationCreate(kCFAllocatorDefault, 0, 0, &error, (CFDictionaryRef)dict);
    if (error != 0) {
        [NSException raise:NSInvalidArgumentException format:@"error ID:%d", error];
        return nil;
    }
    return self;
}

- (void) runInRunLoop:(NSRunLoop*)runLoop andDelegate:(id)delegate
{
    _delegate = delegate;
    CFRunLoopSourceRef source = CFUserNotificationCreateRunLoopSource(NULL,(CFUserNotificationRef)self, asynchronousCallBack, 0);
    CFRunLoopAddSource([runLoop getCFRunLoop],source,kCFRunLoopCommonModes);
}

- (void) updateWithDictionary:(NSDictionary*)dictionary
{
//    CFUserNotificationUpdate((CFUserNotificationRef)self, timeout, flags, (CFDictionaryRef)dictionary);
}

- (int) recieveResponseBeforeTimeout:(double)timeout
{
    CFOptionFlags response = 0;
    CFUserNotificationReceiveResponse((CFUserNotificationRef)self, timeout, &response);
    return response;
}

- (void) cancel
{
    CFUserNotificationCancel((CFUserNotificationRef)self);
}

- (NSDictionary*) textValuesDictionary
{
    return (NSDictionary*)CFUserNotificationGetResponseDictionary((CFUserNotificationRef)self);
}

- (NSDictionary*) responseDictionary
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:[self textValuesDictionary]];
    NSMutableArray *checkArray = [NSMutableArray array];

    int i;
    for (i = 0; i < 8; i++) {
        CFOptionFlags flag = CFUserNotificationCheckBoxChecked(i);
        NSLog(@"%d", flag);
//        [array addObject:];
    }
    [dict setObject:checkArray forKey:@"CheckBoxValues"];
//    [dict setObject:CFUserNotificationPopUpSelection(<#CFIndex n#>) forKey:<#(id)aKey#>
    return dict;
}

@end
